package com.tsingtao.arch.rule;

import static com.societegenerale.commons.plugin.utils.ArchUtils.importAllClassesInPackage;
import static com.tngtech.archunit.base.DescribedPredicate.not;
import static com.tngtech.archunit.core.domain.JavaClass.Predicates.INTERFACES;
import static com.tngtech.archunit.core.domain.JavaClass.Predicates.resideInAnyPackage;
import static com.tngtech.archunit.core.domain.JavaClass.Predicates.resideOutsideOfPackages;
import static com.tngtech.archunit.core.domain.JavaClass.Predicates.simpleName;
import static com.tngtech.archunit.core.domain.JavaClass.Predicates.simpleNameEndingWith;
import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.classes;

import com.societegenerale.commons.plugin.rules.ArchRuleTest;
import com.societegenerale.commons.plugin.service.ScopePathProvider;
import com.tngtech.archunit.base.DescribedPredicate;
import com.tngtech.archunit.core.domain.JavaClass;
import java.util.Collection;

/**
 * Api 规则检查
 */
public class ApiRulesTest implements ArchRuleTest {

  public static final DescribedPredicate<JavaClass> API = INTERFACES
      .and(resideInAnyPackage("..api..", "..thirdparty.."))
      .and(resideOutsideOfPackages("..dto..", "..constants.."));

  @Override
  public void execute(String packagePath, ScopePathProvider scopePathProvider, Collection<String> excludedPaths) {
    classes().that(API).or(simpleNameEndingWith("Api").and(not(simpleName("Api")))).should()
        .haveSimpleNameEndingWith("Api").andShould()
        .resideInAnyPackage("..api..", "..thirdparty..").andShould()
        .beAnnotatedWith("org.springframework.cloud.openfeign.FeignClient")
        .check(importAllClassesInPackage(scopePathProvider.getMainClassesPath(), packagePath, excludedPaths));
  }

}